﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Update_RZCodeCustomsDate : Form
    {
        public string RZCode;
        private INISettings mySettings;
        private SqlConnection myConnection;

        public frm_Update_RZCodeCustomsDate()
        {
            InitializeComponent();            
            this.mySettings = new INISettings();
            this.myConnection = new SqlConnection(mySettings.SQLConnectionString);
            this.RZCode = null;
            LoadRZCode();
        }       

        /////////////////////////////////////////////
        /// Load RZCode List
        /////////////////////////////////////////////
        private void LoadRZCode()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT LEFT([RZCode], 8) AS RZCode FROM [241_RZCode]"
                    + " WHERE CusDate IS NULL AND CusSheetNo IS NULL";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["RZCode"].ToString() + "-xxx";
                    item.Value = dataReader["RZCode"].ToString();
                    this.rzCode.Properties.Items.Add(item);
                }
                dataReader.Close();
                myConnection.Close();                
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get RZCode list");                
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            string error = null;

            string rzMaster = null;
            DateTime customsdate = new DateTime();
            string sheetno = null;

            if (this.rzCode.SelectedIndex >= 0)
            {
                ComboboxItem item = (ComboboxItem)this.rzCode.SelectedItem;
                rzMaster = item.Value.ToString();
                RZCode = rzMaster.Substring(0, 8);
            }
            else
            {
                error = "Please select RZ Code.";
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.customsDate.EditValue == null)
                {
                    error = "Please select Customs Date";
                }
                else
                {
                    customsdate = DateTime.Parse(this.customsDate.EditValue.ToString());
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.sheetNo.Text))
                {
                    error = "Please enter Customs Sheet No.";
                }
                else
                {
                    sheetno = this.sheetNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {                    
                    myConnection.Open();
                    string sql = "UPDATE [241_RZCode] SET CusDate=@customsdate, CusSheetNo='" + sheetno + "'"
                            + " WHERE LEFT(RZCode, 8) = '" + RZCode + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@customsdate", SqlDbType.DateTime).Value = customsdate.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Update Success!");                    
                    //clearForm();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Update failed!");
                }
            }
            else
            {
                MessageBox.Show(error);
            }

        }
    }
}
